
package com.hlkj.pay.controller.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.common.IDownloadExcelService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.order.RiskOrderQueryDto;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantInfoDO;
import com.hlkj.pay.infrastructure.model.order.OrderRiskDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderInfoDO;
import com.hlkj.pay.service.merchant.IMerchantApplicationService;
import com.hlkj.pay.service.merchant.IMerchantService;
import com.hlkj.pay.service.order.IOrderRiskService;
import com.hlkj.pay.service.pay.IPayChannelService;
import com.hlkj.pay.service.pay.IProviderService;
import com.hlkj.pay.util.DateUtils;
import com.hlkj.pay.vo.order.req.ComplaintDeleteReq;
import com.hlkj.pay.vo.order.req.RiskOrderPageQueryReq;
import com.hlkj.pay.vo.order.resp.export.OrderRiskExport;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/10/23 11:36
 */
@Tag(name = "订单风险管理")
@RestController
@RequestMapping("/v1/order/risk")
@RequiredArgsConstructor
@Slf4j
public class RiskOrderController extends BaseController {

    private final IOrderRiskService orderRiskService;

    private final IDownloadExcelService downloadExcelService;

    private final IMerchantService merchantService;

    private final IProviderService providerService;

    private final IPayChannelService payChannelService;

    private final IMerchantApplicationService merchantApplicationService;

    @Operation(summary = "风险列表")
    @PostMapping("/page")
    CommonResult<PageResult<OrderRiskDO>> queryRiskPage(@RequestBody RiskOrderPageQueryReq riskOrderPageQueryReq) {
        RiskOrderQueryDto riskOrderQueryDto = BeanUtil.copy(riskOrderPageQueryReq, RiskOrderQueryDto.class);
        processQueryDate(riskOrderPageQueryReq, riskOrderQueryDto);
        // 导出
        if (riskOrderPageQueryReq.getExport() == 1) {
            String fileName = riskOrderPageQueryReq.getExportName();
            if(StrUtil.isEmpty(fileName)){
                fileName = "订单风险导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            }
            downloadExcelService.createExcel(fileName, riskOrderQueryDto, OrderRiskExport.class);
            return CommonResult.success();
        }
        PageResult<OrderRiskDO> page = orderRiskService.queryPage(riskOrderQueryDto);
        List<OrderRiskDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        for(OrderRiskDO orderRiskDO:list){
            if(StrUtil.isNotEmpty(orderRiskDO.getChannelCode())){
                PayChannelDO payChannelDO = payChannelService.queryByCode(orderRiskDO.getChannelCode());
                if(payChannelDO != null){
                    orderRiskDO.setChannelName(payChannelDO.getName());
                }
            }
            if(StrUtil.isNotEmpty(orderRiskDO.getProviderCode())){
                ProviderInfoDO providerInfoDO = providerService.queryByCode(orderRiskDO.getProviderCode());
                if(providerInfoDO != null){
                    orderRiskDO.setProviderName(providerInfoDO.getShortName());
                }
            }
            if(StrUtil.isNotEmpty(orderRiskDO.getSn())){
                MerchantInfoDO merchantInfoDO = merchantService.queryMerchant(orderRiskDO.getSn());
                if(merchantInfoDO != null){
                    orderRiskDO.setSnName(merchantInfoDO.getName());
                }
            }
            if(orderRiskDO.getAccessId() != null){
                MerchantAppChannelDO merchantAppChannelDO = merchantApplicationService.queryDetailByAccessId(orderRiskDO.getAccessId(),
                        MerchantEnum.DATA_RELATION_TYPE.ACCESS);
                if(merchantAppChannelDO != null){
                    orderRiskDO.setAppChannelCode(merchantAppChannelDO.getCode());
                    orderRiskDO.setAppChannelSubMchId(merchantAppChannelDO.getSubMchId());
                }
            }
        }
        return buildPageResult(page, list);
    }

    @Operation(summary = "风险详情")
    @GetMapping("/detail")
    CommonResult<OrderRiskDO> detail(@RequestParam("id") Long id) {
        OrderRiskDO orderRiskDO = orderRiskService.queryDetail(id);
        if(StrUtil.isNotEmpty(orderRiskDO.getChannelCode())){
            PayChannelDO payChannelDO = payChannelService.queryByCode(orderRiskDO.getChannelCode());
            if(payChannelDO != null){
                orderRiskDO.setChannelName(payChannelDO.getName());
            }
        }
        if(StrUtil.isNotEmpty(orderRiskDO.getProviderCode())){
            ProviderInfoDO providerInfoDO = providerService.queryByCode(orderRiskDO.getProviderCode());
            if(providerInfoDO != null){
                orderRiskDO.setProviderName(providerInfoDO.getShortName());
            }
        }
        if(StrUtil.isNotEmpty(orderRiskDO.getSn())){
            MerchantInfoDO merchantInfoDO = merchantService.queryMerchant(orderRiskDO.getSn());
            if(merchantInfoDO != null){
                orderRiskDO.setSnName(merchantInfoDO.getName());
            }
        }
        if(orderRiskDO.getAccessId() != null){
            MerchantAppChannelDO merchantAppChannelDO = merchantApplicationService.queryDetailByAccessId(orderRiskDO.getAccessId(),
                    MerchantEnum.DATA_RELATION_TYPE.ACCESS);
            if(merchantAppChannelDO != null){
                orderRiskDO.setAppChannelCode(merchantAppChannelDO.getCode());
                orderRiskDO.setAppChannelSubMchId(merchantAppChannelDO.getSubMchId());
            }
        }
        return CommonResult.success(orderRiskDO);
    }

    @Operation(summary = "风险删除")
    @PostMapping("/delete")
    CommonResult<Void> deleteRisk(@RequestBody ComplaintDeleteReq complaintDeleteReq) {
        orderRiskService.delBatch(complaintDeleteReq);
        return CommonResult.success();
    }

    // @PostMapping("/test")
    // CommonResult<Void> test(@RequestBody WxRiskNotifyResp notifyResp) {
    //     return orderRiskService.test(notifyResp);
    // }

}
